package com.hs.kfk.serializer;

import org.apache.kafka.common.serialization.Deserializer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;


/**
 * @Description: User对象的反序列化类，按位取值
 * @Author 胡尚
 * @Date: 2024/8/8 12:52
 */
public class UserDeserializer implements Deserializer<User> {
    @Override
    public User deserialize(String topic, byte[] data) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(data);
        long uid = byteBuffer.getLong();
        int userNameSize = byteBuffer.getInt();
        String username = new String(byteBuffer.get(data, 8 + 4, userNameSize).array(), StandardCharsets.UTF_8).trim();
        int age = byteBuffer.getInt();
        return new User(uid, username, age);
    }
}
